<template>
  <div class="daily">
    <div class="daily-menu">
      <div
        class="daily-menu-item"
        :class="{ on: type === 'recommend' }"
        @click="handleToRecommend"
      >
        每日推荐
      </div>
      <div
        class="daily-menu-item"
        :class="{ on: type === 'daily' }"
        @click="showThemes = !showThemes"
      >
        主题日报
      </div>
      <ul v-show="showThemes">
        <li v-for="item in subList" :key="item.id">
          <a href="#">{{ item.title }}</a>
        </li>
        <!-- <li><a href="#">哈哈</a></li>
        <li><a href="#">哈哈</a></li>
        <li><a href="#">哈哈</a></li>
        <li><a href="#">哈哈</a></li> -->
      </ul>
    </div>
    <div class="daily-list" ref="list" @scroll="handleScroll">
      <template v-if="type === 'recommend'">
        <div v-for="(list, index) in recommendList" :key="index">
          <div class="daily-date">{{ dateFormat(list.date) }}</div>
          <Item
            v-for="item in list.stories"
            :key="item.id"
            :data="item"
            @click.native="handleClick(item.id)"
          ></Item>
        </div>
      </template>
    </div>
    <daily-acticle :id="articleId" />
  </div>
</template>

<script>
import Item from "./components/Item.vue";
import dailyActicle from "./components/dailyActicle.vue";
import $ from "./utils/request";
import dateUtil from "./utils/dateUtil";
import subList from "./mock/subList";
export default {
  name: "App",
  components: {
    Item,
    dailyActicle,
  },
  data() {
    return {
      themes: [],
      subList,
      recommendList: [],
      dailyTime: dateUtil.getDayTime(),
      showThemes: false,
      type: "recommend",
      themeId: 0,
      articleId: 0,
    };
  },
  created() {
    this.getThemes();
    this.getrecommendList();
  },
  computed: {
    dateFormat() {
      return (value) => {
        return dateUtil.formatDay(value);
      };
    },
  },
  methods: {
    getThemes() {
      $.request.get("news/latest").then((res) => {
        console.log(res);
      });
    },
    getrecommendList() {
      const prevDay = dateUtil.prevDay(this.dailyTime + 86400000);
      $.request.get("news/before/" + prevDay).then((res) => {
        console.log(res);
        this.recommendList.push(res);
      });
    },
    handleToRecommend() {
      this.type = "recommend";
      this.recommendList = [];
      this.dailyTime = dateUtil.getDayTime();
      this.getrecommendList();
    },
    // 监听list滚动事件
    handleScroll() {
      const $list = this.$refs.list;
      if ($list.scrollTop + document.body.clientHeight >= $list.scrollHeight) {
        this.dailyTime -= 86400000;
        this.getrecommendList();
      }
    },
    // 监听item回调
    handleClick(id) {
      console.log(id);
      this.articleId = id;
    },
  },
};
</script>

<style>
</style>
